var vm = new Vue({
	el : '#rrapp',
	data : {
		q : {
			mobile : ""
		}
	},
	methods : {
		query : function() {
			if (isBlank(this.q.mobile)) {
				alert("请填写手机号");
			}

			$.getJSON(baseURL + "aic/accounting/select/" + this.q.mobile, function(result) {
					var root = [];
					if (result.accounting) {
						root.push(result.accounting);
					} else {
						parent.layer.msg("手机号不存在");
					}
					$.fn.zTree.init($("#dataTree"), setting, root);
			});
		}
	}
});

var setting = {
	data : {
		key : {
			name : "mobile"
		}
	},
	view : {
		addDiyDom : function(treeId, treeNode) {
			var node = $("#" + treeNode.tId + "_a");
			if(treeNode.id) {
				var html = "<span>" + (treeNode.realname ? treeNode.realname : "") + "</span>" +
						   "<span class='mg'><b>上级:</b>" + (treeNode.parentMobile ? "<a onclick='vm.q.mobile="+treeNode.parentMobile+";vm.query();'>" + treeNode.parentMobile : "无") + (treeNode.parentRealname ? "(" + treeNode.parentRealname + ")" : "" ) + "</a></span>" +
						   "<span class='mg'><b>收入:</b>￥" + treeNode.income + "元</span>" +
						   "<span class='mg'><b>支出:</b>￥" + treeNode.paid + "元</span>" +
						   "<span class='mg'><b>净收益:</b>￥" + floatSub(treeNode.income, treeNode.paid) + "元</span>" +
						   "<span class='mg'><b>买入:</b>" + treeNode.buyAic + "AIC</span>" +
						   "<span class='mg'><b>卖出:</b>" + treeNode.sellAic + "AIC</span>" +
						   "<span class='mg'><b>买入矿机:</b>" + treeNode.buyMachine + "台</span>" +
						   "<span class='mg'><b>运行中矿机:</b>" + treeNode.runMachine + "台</span>";
				node.after(html);
				var tree = $.fn.zTree.getZTreeObj(treeId);
				var childs = [];
				if(treeNode.hasOne) {
					childs.push({mobile: "直接推荐", isParent: true, userLevel: 1, mId: treeNode.id});
					if(treeNode.hasTwo) {
						childs.push({mobile:"二级推荐", isParent: true, userLevel: 2, mId: treeNode.id});
						if(treeNode.hasThree) {
							childs.push({mobile:"三级推荐", isParent: true, userLevel: 3, mId: treeNode.id});
						}
					}
					tree.addNodes(treeNode, -1, childs, true);
				}
			}
		}
	},
	callback : {
		onExpand: function(event, treeId, treeNode) {
			if(treeNode.mId) {
				if(!treeNode.isAjaxing) {
					$.getJSON(baseURL + "aic/accounting/subList", {id: treeNode.mId, level: treeNode.userLevel }, function(result) {
						treeNode.isAjaxing = true;
						var tree = $.fn.zTree.getZTreeObj(treeId);
						if(result.page.list.length > 0) {
							$.each(result.page.list, function(i, item) {
								tree.addNodes(treeNode, -1, item, true);
							});
							
							if(result.page.totalPage != result.page.currPage) {
								tree.addNodes(treeNode, -1, {mobile:"点击加载更多", currPage: result.page.currPage, parentId: treeNode.mId, userLevel: treeNode.userLevel}, true);
							}
						}
					});
				}
			}
		},
		onClick: function(event, treeId, treeNode) {
			if(treeNode.currPage) {
				$.getJSON(baseURL + "aic/accounting/subList", {id: treeNode.parentId, level: treeNode.userLevel, page: treeNode.currPage + 1 }, function(result) {
					var tree = $.fn.zTree.getZTreeObj(treeId);
					if(result.page.list.length > 0) {
						$.each(result.page.list, function(i, item) {
							tree.addNodes(treeNode.getParentNode(), -1, item, true);
						});
						if(result.page.totalPage != result.page.currPage) {
							tree.addNodes(treeNode.getParentNode(), -1, {mobile:"点击加载更多", currPage: result.page.currPage, parentId: treeNode.parentId, userLevel: treeNode.userLevel}, true);
						}
						//删除上次的[点击加载更多]按钮
						tree.removeNode(treeNode);
					}
				});
			}
		}
	}
};

//减    
function floatSub(arg1,arg2){    
    var r1,r2,m,n;    
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}    
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}    
    m=Math.pow(10,Math.max(r1,r2));    
    //动态控制精度长度    
    n=(r1>=r2)?r1:r2;    
    return ((arg1*m-arg2*m)/m).toFixed(n);    
}
